package com.cn.yugan.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cn.yugan.Engine.Beans.NoticeMessageBeans;
import com.cn.yugan.Engine.Json.NoticeMessageJsonObj;
import com.cn.yugan.Engine.Json.NoticeNormalMessageJsonObj;
import com.cn.yugan.Engine.Beans.NoticeNormalBean;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.mobile.R;

import java.util.List;

public class InnerMessageInfoListActivity extends AppCompatActivity {

    final public static int MODE_REPLAYME = 0;
    final public static int MODE_ATME = 1;
    final public static int MODE_MESSAGE = 2;

    private ListView mlistview;
    private List<NoticeNormalBean> ndata;
    private List<NoticeMessageBeans> mdata;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inner_message_reply);

        initdata();
        initactionbar();
        initview();
    }

    private void initdata() {
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 0);
        switch (type) {
            case MODE_REPLAYME:
                ndata = ((NoticeNormalMessageJsonObj) intent.getSerializableExtra("data")).getData();
                LogUtils.e(ndata.get(0).getTitle());
                break;
            case MODE_ATME:
                ndata = ((NoticeNormalMessageJsonObj) intent.getSerializableExtra("data")).getData();
                LogUtils.e(ndata.get(0).getTitle());
                break;
            case MODE_MESSAGE:
                mdata = ((NoticeMessageJsonObj) intent.getSerializableExtra("data")).getData();
                LogUtils.e(mdata.get(0).getTitle());
                break;
        }


    }

    private void initview() {
        mlistview = (ListView) findViewById(R.id.inner_message_reply_lv);
        mlistview.setAdapter(new ListviewAdapter());
        switch (type) {
            case MODE_REPLAYME:
                mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(InnerMessageInfoListActivity.this, CommentActivity.class);
                        intent.putExtra("aid", ndata.get(position).getAid() + "");
                        intent.putExtra("cid", ndata.get(position).getCid());
                        startActivity(intent);
                    }
                });
                break;
            case MODE_ATME:
                mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(InnerMessageInfoListActivity.this, CommentActivity.class);
                        intent.putExtra("aid", ndata.get(position).getAid() + "");
                        intent.putExtra("cid", ndata.get(position).getCid());
                        startActivity(intent);
                    }
                });
                break;
            case MODE_MESSAGE:
                mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(InnerMessageInfoListActivity.this, InnerChatMessageActivity.class);
                        intent.putExtra("username", mdata.get(position).getUsername());
                        startActivity(intent);
                    }
                });
                break;
        }
    }

    private void initactionbar() {
        ActionBar actionbar = getSupportActionBar();
        actionbar.setDisplayHomeAsUpEnabled(true);
        switch (type) {
            case MODE_REPLAYME:
                actionbar.setTitle("回复我的");
                break;
            case MODE_ATME:
                actionbar.setTitle("@我的");
                break;
            case MODE_MESSAGE:
                actionbar.setTitle("私信我的");
                break;
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    private class ListviewAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (type == MODE_MESSAGE) {
                return mdata.size();
            } else {
                return ndata.size();
            }
        }

        @Override
        public Object getItem(int position) {
            if (type == MODE_MESSAGE) {
                return mdata.get(position);
            } else {
                return ndata.get(position);
            }
            
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Viewholder viewholder;
            if (convertView == null) {
                viewholder = new Viewholder();
                convertView = View.inflate(InnerMessageInfoListActivity.this, R.layout.list_item_collection, null);
                viewholder.tv_aid = (TextView) convertView.findViewById(R.id.list_item_collection_time);
                viewholder.tv_title = (TextView) convertView.findViewById(R.id.list_item_collection_title);
                viewholder.tv_staus = (TextView) convertView.findViewById(R.id.list_item_collection_aid);
                convertView.setTag(viewholder);
            } else {
                viewholder = (Viewholder) convertView.getTag();
            }
            if (type==MODE_REPLAYME) {
                viewholder.tv_title.setText(ndata.get(position).getTitle());
                viewholder.tv_aid.setText("yg" + ndata.get(position).getAid());
                if (ndata.get(position).getStatus() == 9) {
                    viewholder.tv_staus.setText("未读");
                    viewholder.tv_staus.setTextColor(Color.RED);
                } else {
                    viewholder.tv_staus.setText("已读");
                    viewholder.tv_staus.setTextColor(Color.BLUE);
                }
            }
            
            if (type==MODE_ATME){
                viewholder.tv_title.setText(ndata.get(position).getTitle());
                viewholder.tv_aid.setText("yg" + ndata.get(position).getAid());
                if (ndata.get(position).getStatus() == 1) {
                    viewholder.tv_staus.setText("未读");
                    viewholder.tv_staus.setTextColor(Color.RED);
                } else {
                    viewholder.tv_staus.setText("已读");
                    viewholder.tv_staus.setTextColor(Color.BLUE);
                }
            }
            
            if (type==MODE_MESSAGE){
                String title = mdata.get(position).getTitle();
                String username = mdata.get(position).getUsername();
                boolean isend = mdata.get(position).isend();
                if ("".equals(title)||title==""||title==null){
                    if (isend){
                        viewholder.tv_title.setText("发送到  "+username);
                    }else {
                        viewholder.tv_title.setText("来自    "+username);
                    }
                    
                }else {
                    viewholder.tv_title.setText("系统消息: "+title);
                }
               
                viewholder.tv_aid.setText(mdata.get(position).getTime());
                if (mdata.get(position).getStatus() == 0) {
                    viewholder.tv_staus.setText("未读");
                    viewholder.tv_staus.setTextColor(Color.RED);
                } else {
                    viewholder.tv_staus.setText("已读");
                    viewholder.tv_staus.setTextColor(Color.BLUE);
                }
            }
            return convertView;
        }
    }

    class Viewholder {
        public TextView tv_aid;
        public TextView tv_title;
        public TextView tv_staus;
    }
}
